from rest_framework import serializers
from .models import ParsedQuestion, ParserTask
from documents.models import Document

class DocumentMinimalSerializer(serializers.ModelSerializer):
    """文档的最小序列化器，用于嵌套在其他序列化器中"""
    file_type = serializers.CharField(source='file_extension')
    
    class Meta:
        model = Document
        fields = ['id', 'title', 'file_type']

class ParsedQuestionSerializer(serializers.ModelSerializer):
    """解析题目的序列化器"""
    class Meta:
        model = ParsedQuestion
        fields = ['id', 'document', 'content', 'answer', 'detected_answer', 'page_number', 'question_type', 'created_at']
        read_only_fields = ['created_at']

class ParserTaskSerializer(serializers.ModelSerializer):
    """解析任务的序列化器"""
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    document_title = serializers.CharField(source='document.title', read_only=True)
    document_file_type = serializers.CharField(source='document.file_extension', read_only=True)
    
    class Meta:
        model = ParserTask
        fields = ['id', 'document', 'document_title', 'document_file_type', 'status', 'status_display', 'created_at', 
                 'completed_at', 'questions_count', 'error_message', 'auto_detect_answers']
        read_only_fields = ['created_at', 'completed_at', 'questions_count', 'error_message', 'status_display', 
                           'document_title', 'document_file_type'] 